# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import gauge
import re


gauge50 = pd.read_table(r"F:/Research/Data/Gauge/GaugeLocation_50.txt",
                        sep="\n", header=None)
gauge229 = pd.read_table(r"F:/Research/Data/Gauge/GaugeLocation_229.txt",
                         sep="\n", header=None)
filter = gauge229.isin(gauge50)
gauge50Loc = []
for i in range(len(gauge50)):
    filter = gauge229[gauge229 == gauge50.ix[i]].dropna().index
    gauge50Loc.extend(list(filter + 6))

# read gauge data
# timeStart = r"2008-01-19_12_00_00"
# timeEnd = r"2008-01-22_00_00_00"
# timeStart = r'2008-03-15_00_00_00'
# timeEnd = r'2008-03-16_12_00_00'
# timeStart = r'2008-09-29_00_00_00'
# timeEnd = r'2008-10-02_06_00_00'
timeStart = r'2008-12-12_00_00_00'
timeEnd = r'2008-12-14_06_00_00'
# read time
month = {0: 0, 1: 31, 2: 29, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30,
         10: 31, 11: 30, 12: 31}
start = re.split('-|_|:', timeStart)
start = [int(s) for s in start]
end = re.split('-|_|:', timeEnd)
end = [int(e) for e in end]
# calculate days of February
if start[1] >= 2:
    if start[0] % 4 == 0:
        month[2] = 29
    else:
        month[2] = 28
startIndex = (np.sum([month[i] for i in range(start[1])]) +
              (start[2] - 1)) * 24 + start[3]
# + 1 means including the last hour
endIndex = (np.sum([month[i] for i in range(end[1])]) +
            (end[2] - 1)) * 24 + end[3]

gauge = gauge.Read(r"F:/Research/Data/Gauge/Gauge229_07_10_1h.dat", 229)
subset2008 = gauge[gauge.index == start[0]]
rng = pd.date_range('1/1/2008', periods=8784, freq='H')
subset2008.index = rng

subset50 = subset2008.iloc[startIndex:(endIndex + 1), gauge50Loc]
negative = (subset50 == -1)
col = negative.any(axis=0)
print(subset50.loc[:, col])
